package com.teach.servlet.admin;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teach.entity.Lj;
import com.teach.service.LjService;
import com.teach.service.impl.LjServiceImpl;

//@WebServlet("/admin/lj-add.let")
public class LjAddServlet extends HttpServlet{

	private LjService ljSerivice = new LjServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		Lj lj = new Lj();
		lj.setLjName(request.getParameter("ljName"));
		lj.setLjNickname(request.getParameter("ljNickname"));
		lj.setLjPassword(request.getParameter("ljPassword"));
		lj.setLjPhone(request.getParameter("ljPhone"));
		lj.setLjQq(request.getParameter("ljQq"));
		lj.setLjSex(Boolean.valueOf(request.getParameter("ljSex")));
		try {
			lj.setLjBrithdate(new SimpleDateFormat("yyyy-mm-dd").parse(request.getParameter("ljBrithdate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
//		lj.setLjAddress(request.getParameter("ljAddress"));//数字
		if(Integer.valueOf(request.getParameter("ljAddress"))==1) {
			lj.setLjAddress("湖南");
		}else if(Integer.valueOf(request.getParameter("ljAddress"))==2) {
			lj.setLjAddress("湖北");
		}
		
		String[] arr = request.getParameterValues("ljLoves");
		for (String loves : arr) {
			if(Integer.valueOf(loves)==1) {
				lj.setLjLoves("喜欢编程");
			}
			if(Integer.valueOf(loves)==2) {
				lj.setLjLoves("还是编程");
			}
			if(Integer.valueOf(loves)==3) {
				lj.setLjLoves("就是编程");
			}
			if(Integer.valueOf(loves)==4) {
				lj.setLjLoves("只爱编程");
			}
		}
		
		try {
			ljSerivice.insert(lj);
			request.setAttribute("msg", "添加用户成功");
			request.setAttribute("href", "admin/lj-list.let");
		}catch(Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
		}
		request.getRequestDispatcher("/admin/msg.jsp").forward(request, response);
	}
}

